export function buildTree(listData, titleName, idName, parentIdName) {
  let tree = listData.map((item) => ({
    id: item[idName],
    title: item[titleName],
    children: [],
    loading: false,
    parent: null,
    data: item,
  }));

  for (let i = tree.length - 1; i >= 0; i--) {
    const node = tree[i];
    const parent = findInTreeById(tree, node.data[parentIdName]);
    if (!!parent) {
      tree.splice(i, 1);
      parent.children.push(node);
      node.parent = parent;
    }
  }
  return tree;
}

export function findInTreeById(tree, id) {
  let ret = tree.find((node) => node.id === id);
  if (!ret) {
    for (let i = 0, len = tree.length; i < len; i++) {
      ret = findInTreeById(tree[i].children, id);
      if (!!ret) {
        break;
      }
    }
  }
  return ret;
}